﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GeneralClasses;

namespace Interface
{
    public partial class frmSkills : Form
    {
        Form opener;
        List<Skill> skills;
        Skill selectedSkill;

        public frmSkills(Form Opener)
        {
            InitializeComponent();
            opener = Opener;
        }

        public void refresh()
        {
            selectedSkill = null;

            skills = Skill.getAllSkills();

            txtAddSkillName.Text = "";
            txtEditSkillName.Text = "";

            lstboxSkills.Items.Clear();
            foreach (Skill item in skills)
            {
                lstboxSkills.Items.Add(item.Name);
            }
        }

        private void frmSkills_FormClosing(object sender, FormClosingEventArgs e)
        {
            opener.Show();
        }

        private void frmSkills_Shown(object sender, EventArgs e)
        {
            refresh();
        }

        private void lstboxSkills_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (Skill item in skills)
            {
                if (item.Name == lstboxSkills.SelectedItem.ToString())
                {
                    selectedSkill = item;
                    break;
                }
            }

            if (selectedSkill != null)
            {
                txtEditSkillName.Text = selectedSkill.Name;
            }
        }

        private void btnAddSkill_Click(object sender, EventArgs e)
        {
            if (txtAddSkillName.Text == "")
            {
                MessageBox.Show("Please enter a name for the skill.");
                txtAddSkillName.Focus();
            }
            else
            {
                selectedSkill = new Skill();
                selectedSkill.Name = txtAddSkillName.Text;

                if (selectedSkill.saveData() > 0)
                {
                    refresh();
                }
                else
                {
                    MessageBox.Show("Problem adding skill!");
                }
            }
        }

        private void btnEditSkill_Click(object sender, EventArgs e)
        {
            if (selectedSkill != null)
            {
                selectedSkill.Name = txtEditSkillName.Text;

                if (selectedSkill.saveData() > 0)
                {
                    refresh();
                }
                else
                {
                    MessageBox.Show("Problem editing skill!");
                }
            }
        }

        private void btnDeleteSkill_Click(object sender, EventArgs e)
        {
            if (selectedSkill != null)
            {
                if (selectedSkill.removeData() == 0)
                {
                    refresh();
                }
                else
                {
                    MessageBox.Show("Problem deleting skill!");
                }
            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtAddSkillName_TextChanged(object sender, EventArgs e)
        {
            Input_Constraint.keepOnly(txtAddSkillName, true, true, true, false, null);
        }

        private void txtEditSkillName_TextChanged(object sender, EventArgs e)
        {
            Input_Constraint.keepOnly(txtEditSkillName, true, true, true, false, null);
        }
    }
}
